﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xcap
{
    public class XcapRequest
    {
        private Config config;                          //konfiguracia
        private Account account;                        //ucet
        private xcapAction requestType;                 //typ transakcie
        private bool auth;                              //ci prebehne autentifikacia
        private string httpUri = "";                    //subor ktory sa spracuje
        private string connection = "Keep-Alive";       //default, neviem co presne robi
        private string xmlContent;                      //obsah xml suboru, ktory pojde na server
        private string httpVersion = "1.1";

        #region properties

        /// <summary>
        /// Points to a file in server
        /// </summary>
        public string HttpUri
        {
            get { return httpUri; }
            set { httpUri = value; }
        }
        /// <summary>
        /// Type of http request
        /// </summary>
        public xcapAction RequestType
        {
            set { requestType = value; }
        }
        /// <summary>
        /// Required authorization?
        /// </summary>
        public bool Auth
        {
            set { auth = value; }
        }
        /// <summary>
        /// XML content being uploaded to a server
        /// </summary>
        public string XmlContent
        {
            set { xmlContent = value; }
        }
        /// <summary>
        /// Version of used HTTP protocol (default 1.1)
        /// </summary>
        public string HttpVersion
        {
            set { httpVersion = value; }
        }


        #endregion

        public XcapRequest(Config _config, Account _account)
        {
            config = _config;
            account = _account;
            //doc.getHeader(xcapDocument.presRules);
            //header.getHeader(xcapDocument.presRules);
        }

        /// <summary>
        /// Method generates http string request
        /// </summary>
        /// <param name="document">xcapDocument document type</param>
        /// <returns>Desired http request with required headers</returns>
        public string BasicRequest(xcapDocument document)
        {
            string request = requestType.ToString() + " /" + config.ServerRoot + httpUri + " HTTP/" + httpVersion + "\r\n";
            if (requestType == xcapAction.PUT)
            {
                request += "Content-Type: " + XcapHeaders.header[(int)document] + "\r\n";
                request += "Accept-Encoding: identity\r\n";
                request += "Content-Length: " + Convert.ToString(xmlContent.Length) + "\r\n"; //wtf? preco mi tu dava stale nulu? aj ked tam explicitne napisem cislo, shit
                //Console.WriteLine(xmlContent.Length);
                request += "Connection: " + connection + "\r\n";
            }
            request += "User-Agent: " + config.UserAgent + "\r\n"
                + "Host: " + config.Host + "\r\n"
                + "Connection: " + connection + "\r\n";
            if (auth == true)
            {
                string hash = Base64Encode(account.Login + ":" + account.Password);
                request += "Authorization: Basic " + hash + "\r\n";
            }
            //vzdy musi byt prazdny riadok 
            request +=
                 "\r\n";
            
            //ak je requestType PUT ziskame obsah ktory chceme poslat na server
            if (requestType == xcapAction.PUT)
            {
                request += xmlContent;
            }
            //Console.WriteLine(request);
            return request;
        }

        ////nepouzite zatial
        //public string BasicRequest(bool auth,string login, string password)
        //{
        //    string hash = "";

        //    if (auth == true)
        //    {
        //        hash = Base64Encode(login + ":" + password);
        //    }

        //    string request = requestType + " /" + httpUri + " HTTP/1.1\r\n"
        //         //+ "User-Agent: " + userAgent + "\r\n"
        //         //+ "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
        //         + "Host: " + config.Host + "\r\n"
        //         + "Connection: " + connection + "\r\n"
        //         + "Authorization: Basic " + hash + "\r\n"
        //         + "\r\n";

        //    return request;
        //}

        private string Base64Encode(string data)
        {
            try
            {
                byte[] encData_byte = new byte[data.Length];
                encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
                string encodedData = Convert.ToBase64String(encData_byte);
                return encodedData;
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Encode" + e.Message);
            }
        }
    }
}
